package cn.tedu.eb_film.controller;


import cn.tedu.eb_film.pojo.dto.UserCommentDTO;
import cn.tedu.eb_film.pojo.entity.UserComment;
import cn.tedu.eb_film.pojo.vo.UserCommentStandardVO;
import cn.tedu.eb_film.service.UserCommentService;
import cn.tedu.eb_film.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 评论表(UserComment)表控制层
 *
 * @author zhaohaoyu
 * @since 2023-03-14 18:53:44
 */
@RestController
@RequestMapping("userComment")
@Api(tags = "用户评论")
@Slf4j
public class UserCommentController {
    /**
     * 服务对象
     */
    @Autowired
    private UserCommentService userCommentService;


    /**
     * 新增数据
     *
     * @param userComment 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public JsonResult<Void> add(UserComment userComment) {
        userCommentService.insert(userComment);
        return JsonResult.ok();
    }

    @PostMapping("addNew")
    @ApiOperation("添加评论及评分")
    public JsonResult<Void> addNew(@RequestBody UserCommentDTO user){
        log.debug("参数为{}",user);
        userCommentService.addNew(user);
        return JsonResult.ok();
    }


    /**
     * 批量添加评论
     *
     * @param list
     * @return
     */
    @PostMapping("/addList")
    public JsonResult<Void> addList(List<UserComment> list) {
        userCommentService.addList(list);
        return JsonResult.ok();
    }

    /**
     * 通过id删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteById")
    public JsonResult<Void> deleteById(Long id) {
        userCommentService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 通过ids删除数据
     *
     * @param list
     * @return
     */
    @PostMapping("/deleteByIdList")
    public JsonResult<Void> deleteByIdList(List<Long> list) {
        userCommentService.deleteByIdList(list);
        return JsonResult.ok();
    }

    @GetMapping("/count")
    public JsonResult<Void> count(UserComment userComment) {
        userCommentService.count(userComment);
        return JsonResult.ok();
    }
    @GetMapping("/{id:[0-9]+}/avgScore")
    @ApiOperation("查询平均数评分")
    public JsonResult<BigDecimal> avgScore(@PathVariable Long id){
        BigDecimal avgScore = userCommentService.avgScore(id);
        return JsonResult.ok(avgScore);

    }

    @GetMapping("/{id:[0-9]+}/getUsername")
    @ApiOperation("连表查询用户信息")
    public JsonResult<List<UserCommentStandardVO>> getUsername(@PathVariable Long id){
        log.debug("ID为{}",id);
        List<UserCommentStandardVO> userName = userCommentService.getUserName(id);
        log.debug("查询结果{}",userName);
        return JsonResult.ok(userName);
    }


    /**
     * 通过id查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id:[0-9]+}/queryById")
    public JsonResult<List<UserComment>> queryById(@PathVariable Long id) {
        List<UserComment> list = userCommentService.queryById(id);

        return JsonResult.ok(list);
    }

    /**
     * 获取用户评论列表
     * @return
     */
    @GetMapping("/getCommentList")
    public JsonResult<List<UserComment>> getCommentList(){
        List<UserComment> list = userCommentService.getCommentList();
        return JsonResult.ok(list);
    }

    @PostMapping("/updateComment")
    public JsonResult<Void> updateComment(UserComment userComment){
        userCommentService.update(userComment);
        return JsonResult.ok();
    }



}

